/*
 * Copyright 2009 Red Hat, Inc.
 *
 * Red Hat licenses this file to you under the Apache License, version 2.0
 * (the "License"); you may not use this file except in compliance with the
 * License.  You may obtain a copy of the License at:
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package com.easy.stock.spider;

import java.io.IOException;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import org.jboss.netty.handler.codec.http.HttpChunk;
import org.jboss.netty.handler.codec.http.HttpResponse;
import com.easy.stock.spider.SpiderTask.STATUS;
import com.easy.stock.spider.html.BaseHtmlSpider;

public abstract class HttpResponseHandler extends SimpleChannelUpstreamHandler {

	protected static Log LOG = LogFactory.getLog("spider");
	protected BaseHtmlSpider spider;
	protected SpiderTask task;
    private boolean readingChunks;
    
	/**
	 * 抓取失败的处理函数
	 * 
	 * @param t
	 */
	public abstract void doOnThrowable(Throwable t);
	
	/**
	 * 抓取成功的处理函数
	 * 
	 * @param response	返回的结果
	 * @return List<SpiderTask>	由该任务衍生出来的继续抓取的任务
	 */
	public abstract List<SpiderTask> doOnCompleted(HttpResponse response) throws IOException;
    

    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
        if (!readingChunks) {
            HttpResponse response = (HttpResponse) e.getMessage();
            if (response.isChunked()) {
                readingChunks = true;
            } else {
            	// 调用业务逻辑
        		List<SpiderTask> tasks=null;
        		try {
        			tasks = doOnCompleted(response);
        			// 修改task的状态
        			task.setStatus(STATUS.SUCCESS);
        			spider.offTask(task);
        		} catch (Throwable t) {
        			task.setStatus(STATUS.FAILED);
        			spider.offTask(task);
        			LOG.error("process response url exception: "+task.getUrl()+" ,reason=", t);
        			
        		}
        		// 把tasks加入队列
        		if(tasks!=null) {
        			for(SpiderTask task : tasks) {
        				spider.offTask(task);
        			}
        			tasks.clear();
        			tasks=null;
        		}
            }
        } else {
            HttpChunk chunk = (HttpChunk) e.getMessage();
            if (chunk.isLast()) {
                readingChunks = false;
            } 
        }
    }
    
    
    public void exceptionCaught(
            ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
    	
    	// 修改task的状态
    	task.setStatus(STATUS.FAILED);
    	spider.offTask(task);
    	LOG.error("exception when crawl url:"+task.getUrl()+", reason=", e.getCause());
    	// 调用业务处理逻辑
    	doOnThrowable(e.getCause());
    }
    
    public SpiderTask getTask() {
		return task;
	}

	public void setTask(SpiderTask task) {
		this.task = task;
	}
}
